package com.xcity.game.skill;

import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.employee.EmployeeUtils;
import com.xcity.game.employee.template.EmployeeTemplate;
import com.xcity.game.skill.template.SkillTemplate;
import com.xcity.game.skill.v2.DefaultSkillV2;
import com.xcity.game.skill.v2.SkillManagerV2;
import com.xcity.pomelo.response.SkillV2Info;

public class SkillUtils {

	// 玩家主动技能
	public static DefaultSkillV2 createActiveSkill(Player player) {
		EmployeeTemplate et = EmployeeUtils.getPlayerTemplate();
		return new DefaultSkillV2(player, et.getActiveSkillTemplate(), 0);
	}
	
	// 玩家被动技能
	public static List<DefaultSkillV2> createPassiveSkills(Player player) {
		EmployeeTemplate et = EmployeeUtils.getPlayerTemplate();
		int[] unlockStarLvls = et.getPassiveSkillUnlockStarLvls(); // 各技能对应解锁等级
		List<SkillTemplate> sts = et.getPassiveSkillTemplates();
		List<DefaultSkillV2> skills = new ArrayList<>(sts.size());
		for (int i = 0; i < sts.size(); i++) {
			DefaultSkillV2 skill = new DefaultSkillV2(player, sts.get(i), unlockStarLvls[i]);
			skills.add(skill);
		}
		return skills;
	}
	
	// 员工主动技能
	public static DefaultSkillV2 createActiveSkill(Employee employee) {
		EmployeeTemplate et = employee.getTemplate();
		return new DefaultSkillV2(employee, et.getActiveSkillTemplate(), 0);
	}
	
	// 员工被动技能
	public static List<DefaultSkillV2> createPassiveSkills(Employee employee) {
		EmployeeTemplate et = employee.getTemplate();
		int[] unlockStarLvls = et.getPassiveSkillUnlockStarLvls(); // 各技能对应解锁等级
		List<SkillTemplate> sts = et.getPassiveSkillTemplates();
		List<DefaultSkillV2> skills = new ArrayList<>(sts.size());
		for (int i = 0; i < sts.size(); i++) {
			DefaultSkillV2 skill = new DefaultSkillV2(employee, sts.get(i), unlockStarLvls[i]);
			skills.add(skill);
		}
		return skills;
	}
	
	public static DefaultSkillV2 find(Player player, long unitId, int skillId) {
		if (unitId == -1 || unitId == player.getId()) {
			return player.getSkillManager().get(skillId);
		}
		Employee employee = player.getEmployees().get(unitId);
		if (employee == null) {
			return null;
		}
		return employee.getSkillManager().get(skillId);
	}
	
	public static List<SkillV2Info> toResponse(Player player) {
		return toResponse(player.getSkillManager());
	}
	
	public static List<SkillV2Info> toResponse(Employee employee) {
		return toResponse(employee.getSkillManager());
	}
	
	public static List<SkillV2Info> toResponse(SkillManagerV2 manager) {
		List<SkillV2Info> skills = new ArrayList<>(manager.size());
		// 主动技能
		skills.add(new SkillV2Info(manager.getActive()));
		// 被动技能
		List<DefaultSkillV2> passives = manager.getPassives();
		for (int i = 0, size = passives.size(); i < size; i++) {
			skills.add(new SkillV2Info(passives.get(i)));
		}
		return skills;
	}
	
}
